package org.chen.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.chen.common.constant.ArticleStatusEnum;
import org.chen.common.exception.ServiceException;
import org.chen.domain.dto.AvatarExcludeDTO;
import org.chen.domain.dto.AvatarExcludeQueryDTO;
import org.chen.domain.dto.BatchDeleteExcludeDTO;
import org.chen.domain.entity.AvatarExclude;
import org.chen.domain.entity.VideoArticle;
import org.chen.domain.response.PageResult;
import org.chen.domain.vo.ArticleVO;
import org.chen.mapper.AvatarExcludeMapper;
import org.chen.service.AvatarExcludeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class AvatarExcludeServiceImpl extends ServiceImpl<AvatarExcludeMapper, AvatarExclude> implements AvatarExcludeService {
    @Autowired
    private AvatarExcludeMapper avatarExcludeMapper;

    @Override
    public void saveAvatarName(AvatarExcludeDTO exclude) {
        // 检查是否已存在
        LambdaQueryWrapper<AvatarExclude> wrapper = new LambdaQueryWrapper<AvatarExclude>()
                .eq(AvatarExclude::getAvatarName, exclude.getAvatarName());

        if (avatarExcludeMapper.selectCount(wrapper) > 0) {
            throw new ServiceException("该名称已存在于排除列表中");
        }

        AvatarExclude avatarExclude = new AvatarExclude();
        avatarExclude.setAvatarName(exclude.getAvatarName());
        avatarExcludeMapper.insert(avatarExclude);
    }

    @Override
    public PageResult<AvatarExclude> getExcludeAvatar(AvatarExcludeQueryDTO avatarExcludeQueryDTO) {
        Page<AvatarExclude> page = new Page<>(avatarExcludeQueryDTO.getPageNum(), avatarExcludeQueryDTO.getPageSize());

        LambdaQueryWrapper<AvatarExclude> wrapper = new LambdaQueryWrapper<AvatarExclude>()
                .eq(StringUtils.isNotBlank(avatarExcludeQueryDTO.getAvatarName()), AvatarExclude::getAvatarName, avatarExcludeQueryDTO.getAvatarName())
                .orderByDesc(AvatarExclude::getCreateTime);

        Page<AvatarExclude> resultPage = avatarExcludeMapper.selectPage(page, wrapper);

        return new PageResult<>(resultPage.getRecords(),
                resultPage.getTotal(),
                resultPage.getSize(),
                resultPage.getCurrent());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteByIds(BatchDeleteExcludeDTO ids) {
        if (CollectionUtils.isEmpty(ids.getIds())) {
            return;
        }
        avatarExcludeMapper.deleteByIds(ids.getIds());
    }
}
